import { reqGoodsInfo, reqAddOrUpdateShopCart } from '@/api'
//detail的小仓库
const state = {
    goodInfo: {},
};
const mutations = {
    REQGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo;
    }
};
const actions = {
    async getGoodInfo(context, skuid) {
        let result = await reqGoodsInfo(skuid);
        if (result.code == 200) {
            context.commit('REQGOODINFO', result.data);
        }
    },
    //将产品加到购物车中
    async AddOrUpdateShopCart(context, { skuid, skuNum }) {
        let result = await reqAddOrUpdateShopCart(skuid, skuNum);
        //此结果中没有返回data，不需要commit和mutation两部操作
        //此函数会返回一个Promise对象
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    }
};
const getters = {
    categoryView(state) {
        return state.goodInfo.categoryView || {};
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || [];
    },
};

export default {
    state,
    mutations,
    actions,
    getters,
}